import Vue from 'vue'
import VueRouter from "vue-router"
import VueCookies from 'vue-cookies'

Vue.use(VueRouter)
    .use(VueCookies)

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/',
            component: () => import('@/views/Index')
        },
        {
            path: '/index',
            component: () => import('@/views/Index')
        },
        {
            path: '/login',
            component: () => import('@/views/Login')
        },
        {
            path: '/reg',
            component: () => import('@/views/Register')
        },
        {
            path: '/my-order',
            component: () => import('@/views/MyOrder'),
            props: true
        },
        {
            path: '/my-cart',
            component: () => import('@/views/MyShoppingCart')
        },
        {
            path: '/settle',
            name: 'settle',
            component: () => import('@/views/Settle')
        },
        {
            path: '/introduction',
            component: () => import('@/views/Introduction'),
            redirect: '/introduction/particulars',
            children: [
                {
                    path: '/introduction/particulars',
                    component:  () => import('@/views/Introduction/Particulars'),
                },
                {
                    path: '/introduction/evaluate',
                    component:  () => import('@/views/Introduction/Evaluate'),
                },
                
            ]
        },
    ]
})
/*
*路由守卫，类似与Java中的过滤器 
* to:到哪里去
* from: 从哪里来，几乎不用
* next：是否接着往下走，如果没有参数表示接着往下走，如果有参数(是路由path),就到对应页面
* cookies有两种用法，this.$cookies和vue.$cookies this只能在组件中使用
*/
router.beforeEach((to,from,next)=>{
    if(Vue.$cookies.get('access_token') && Vue.$cookies.get('refresh_token')){
        next();
    }else if(to.path=='/my-cart'||to.path=='/my-order'){
        next('/login');
    }else{
        next();
    }
})

export default router